const app = getApp();
Page({
	data: {
		url: app.globalData.host,
		id: null,
		type: '',
		title: '',
		content: {},
		needArticleList: [],
	},
	onLoad(options) {
		this.setData({
			id: options.id,
			type: options.type
		})
		let url = '';
		let title = '';
		if(options.type == "news"){
			url = '/app/cmsNews/';
			title = "新闻详情";
		}else if(options.type == "notice"){
			url = '/app/notice/';
			title = "公告详情";
		}else if(options.type == "banner"){
			url = '/app/banner/';
			title = "新闻详情";
		}

		this.getList(url);
		wx.setNavigationBarTitle({
			title: title
		})
	},
	getList(url){
		let id = this.data.id;
		wx.showLoading({title: '加载中…'});
		app.request({
			url:  url + id,
			method: 'GET',
			success: res => {
				let content = '';
				let content2 = '';
				let str = '';
				let title = '';
				if(this.data.type == "news"){
					str = res.data.content;
					title = res.data.title;
				}else if(this.data.type == "notice"){
					str = res.data.noticeContent;
					title = res.data.noticeTitle;
				}else if(this.data.type == "banner"){
					str = res.data.content;
					title = res.data.title;
				}

				let reg1 = new RegExp('<img ','g');
				let reg2 = new RegExp('src="/prod-api','g');
				let newStr = str.replace(reg1,'<img style="max-width:100%;height:auto;display:block;margin:10px 0;"');
				content = newStr.replace(reg2,'src="' + this.data.url);

				let regVideoStr = new RegExp('<iframe class="ql-video" frameborder="0" allowfullscreen="true" src="','g');
				let regVideoSrc = new RegExp('</iframe>','g');
				let newVideoStr = content.replace(regVideoStr,'<video class="video" src="');
				content2 = newVideoStr.replace(regVideoSrc,'</video>');

				this.setData({
					content: content2,
					title: title
				})
				this.setList();
			}
		})
		wx.hideLoading();
	},
	setList(){
		let content = this.data.content;

		let videoList = [];
		let videoReg = /<video.*?(?:>|\/>)/gi;   //匹配到字符串中的 video 标签
		let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;  //匹配到字符串中的 video 标签 的路径
		let arr = content.match(videoReg) || [];  // arr 为包含所有video标签的数组
		let articleList = content.split('</video>')   // 把字符串  从视频标签分成数组

		arr.forEach((item,index)=>{
			var src = item.match(srcReg);
			videoList.push(src[1])     //所要显示的字符串中 所有的video 标签 的路径
		})
		let list = [];   
		articleList.forEach((item,index)=>{
			if(item != "" && item != undefined){  //  常见的标签渲染
				list.push({
				type:'rich-text',
				value:item+"</video>"
				});
			}
			let articleListLength = articleList.length;  // 插入到原有video 标签位置
			if(index<articleListLength && videoList[index] != undefined){
				list.push({
				type:'video',
				value:videoList[index]
				})
			}
		})
		this.setData({
			needArticleList: list
		})
	}
})